package com.swpu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swpu.common.CustomException;
import com.swpu.entity.Category;
import com.swpu.entity.Dish;
import com.swpu.entity.Setmeal;
import com.swpu.mapper.CategoryMapper;
import com.swpu.service.CategoryService;
import com.swpu.service.DishService;
import com.swpu.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    //根据id删除分类，需要判断是否关联菜品或套餐
    @Override
    public void remove(Long id) {
        //菜品判断
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int countDish = dishService.count(dishLambdaQueryWrapper);
        if (countDish>0){
            throw new CustomException(("当前分类下关联了菜品，请先取消关联"));
        }

        //套餐判断
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int countSetmeal = setmealService.count(setmealLambdaQueryWrapper);
        if (countSetmeal>0){
            throw new CustomException(("当前分类下关联了套餐，请先取消关联"));
        }

        super.removeById(id);
    }

}
